package ywh.pool
{
	import flash.utils.Dictionary;
	import ywh.event.MediaListEvent;
	import ywh.list.FollowList;
	import ywh.list.MainList;
	import ywh.list.MediaList;
	import ywh.manager.Core;
	import ywh.manager.CoreState;
	import ywh.manager.Tag;
	import ywh.pool.ListPool;
	import ywh.utils.XMLUtils;
	
	/**
	 * 单例类,数据列表池;
	 * @author 杨伟华
	 */
	public class ListPool
	{
		{
			_activeLists = new Vector.<FollowList>();
			_usableLists = new Vector.<FollowList>();
			_listToListInfo = new Dictionary(true);
			Core.addEventListener(MediaListEvent.LIST_READY, listReadyHandle);
		}
		static private var _id2ListsMap:Object;
		static private var _activeLists:Vector.<FollowList>;
		static private var _usableLists:Vector.<FollowList>;
		static private var _listToLoadCount:uint;
		static private var _listToListInfo:Dictionary;
		
		public function ListPool(p:PrivateClass)
		{
			throw(new Error("FollowListPool是一个静态类"));
		}
		
		/**
		 * 所有列表更新;
		 * @param	listInfos	需要加载的列表信息;
		 * @param	chapterPath	章节目录;
		 * @param	basePath	数据路径基本目录;
		 */
		static public function loadLists(listInfos:XMLList, basePath:String = ""):void
		{
			var count:int = _listToLoadCount = listInfos.length();
			destoryLists();
			loadList(mainList, listInfos[0], basePath);
			for (var i:uint = 1; i < count; i++)
			{
				var fml:FollowList = getFollowList();
				loadList(fml, listInfos[i], basePath);
				_activeLists.push(fml);
			}
		}
		
		static public function get mainList():MainList
		{
			return MainList.$;
		}
		
		/**
		 * 根据名称确认列表;
		 * @param	listId
		 * @return
		 */
		static public function getListById(listId:String):MediaList
		{
			return _id2ListsMap[listId] as MediaList;
		}
		
		/**
		 * 流更新列表;
		 * @param	listInfo 列表信息，如果其中的流有名字即含有streamId属性，则为从流，否则为主流;
		 * @param	fullPath 基准路径;
		 */
		static private function loadList(list:MediaList, listInfo:XML, basePath:String):void
		{
			var listId:String = XMLUtils.getPropString(listInfo, Tag._LIST_ID);
			_id2ListsMap[listId] = list;
			_listToListInfo[list] = listInfo;
			var listURL:String =Core.getResolvePath(basePath + XMLUtils.getPropString(listInfo, Tag._URL));
			list.loadList(listURL);
		}
		
		/**
		 * 清空所有流的数据;
		 */
		static private function destoryLists():void
		{
			mainList.destory();
			var fml:FollowList;
			_id2ListsMap = {};
			while (_activeLists.length)
			{
				fml = _activeLists.pop();
				fml.destory();
				_usableLists.push(fml);
			}
		}
		
		static private function getFollowList():FollowList
		{
			if (_usableLists.length > 0)
			{
				return _usableLists.pop();
			}
			else
			{
				return new FollowList();
			}
		}
		
		/**
		 * 当每个列表的
		 * @param	e
		 */
		static private function listReadyHandle(e:MediaListEvent):void
		{
			initPlayers(e.list);
			if (!(--_listToLoadCount))
			{
				Core.state = CoreState.LISTS_DATA_LOADED;
			}
		}
		
		/**
		 * 根据既得列表数据初始化播放器;
		 * @param	list
		 */
		static private function initPlayers(list:MediaList):void
		{
			var initPlayerString:String = XMLUtils.getPropString(_listToListInfo[list], Tag._INIT_PLAYERS);
			if (initPlayerString)
			{
				var initPlayerNames:Array = initPlayerString.split(";");
				for each (var name:String in initPlayerNames)
				{
					PlayerPool.getPlayerById(name).dataProvider = list.xml;
				}
			}
			delete _listToListInfo[list];
		}
	}
}

class PrivateClass
{
}